/*
 * @Description: 
 * @Author: 马爽
 * @Date: 2021-03-04 19:24:45
 * @LastEditTime: 2021-03-10 13:20:14
 * @LastEditors: 马爽
 */

import { Effect, Reducer, Subscription} from 'umi';
import {  
    getUser, 
    getIdentity, 
    getApiAuthority, 
    getIdentityApi,
    view_authority,
    getIdentityView,
    getAddUser,
    updateUsers,
    addIdentity,
    addAuthorityApi,
    authorityView,
    setIdentityApi,
    setIdentityView
} from '@/services/userManage';
import { 
    UserListType,
    IdentityListType,
    View_authority,
    ApiAuthorityList,
} from '@/interface/user';

let tabList = [getUser,getIdentity,getApiAuthority,getIdentityApi,view_authority,getIdentityView];

export interface userManageModelState {
    userList: UserListType[],
    identityList: IdentityListType[],
    viewAuthorityList: View_authority[]
    authorityList: ApiAuthorityList[],
    tabOne:any,
}

export interface UserModelType {
    namespace: 'userManage',
    state: userManageModelState,
    effects: {
        getUserList: Effect,
        getIdentityList: Effect,
        getViewAuthorityList: Effect,
        getAuthorityList: Effect,
        tab: Effect,
        getAddUser: Effect,
        updateUsers: Effect,
        AddIdentity: Effect,
        addAuthorityApi: Effect,
        authorityView: Effect,
        setIdentityApi: Effect,
        setIdentityView: Effect
    },
    reducers: {
        save:Reducer<userManageModelState>
    },
    subscriptions: {setup: Subscription};
}

const UserModel:UserModelType = {
    namespace:'userManage',
    state: {
        userList: [],
        identityList: [],
        viewAuthorityList: [],
        authorityList: [],
        tabOne: [],
    },
    effects: {             
        *getUserList({ payload }, { call, put }){
            let result = yield call(getUser);
            if(result.code === 1){
                yield put({
                    type: 'save',
                    payload: {userList:result.data}
                })
            }
        },
        *getIdentityList({ payload }, { call, put }){
            let result = yield call(getIdentity);
            if(result.code === 1){
                yield put({
                    type: 'save',
                    payload: {identityList:result.data}
                })
            }
        },
        *getViewAuthorityList({ payload }, { call, put }){
            let result = yield call(view_authority);
            if(result.code === 1){
                yield put({
                    type: 'save',
                    payload: {viewAuthorityList:result.data}
                })
            }
        },
        *getAuthorityList({ payload }, { call, put }){
            let result = yield call(getApiAuthority);
            if(result.code === 1){
                yield put({
                    type: 'save',
                    payload: {authorityList:result.data}
                })
            }
        },
        *tab({ payload }, { call, put }){
            let result = yield call(tabList[payload]);
            // console.log(tabList[payload])
            if(result.code === 1){
                // console.log(result.data)
                yield put({
                    type:'save',
                    payload: {tabOne: result.data}
                })
            }
        },
        *getAddUser({ payload }, { call, put }){
            let result = yield call(getAddUser,payload);
            if(result.code === 1){
                alert('添加用户成功')
            }else{
                alert('添加身份失败');
            }
        },
        *updateUsers({ payload }, { call, put }){
            let result = yield call(updateUsers,payload);
            if(result.code === 1){
                alert('成功')
            }else{
                alert('失败');
            }
        },
        *AddIdentity({ payload }, { call, put }){
            let result = yield call(addIdentity,payload);
            if(result.code === 1){
                alert('添加身份成功');
            }else{
                alert('添加身份失败');
            }
        },
        *addAuthorityApi({ payload }, { call, put }){
            let result = yield call(addAuthorityApi,payload);
            if(result.code === 1){
                alert('添加成功');
            }else{
                alert('添加失败');
            }
        },
        *authorityView({ payload }, { call, put }){
            let result = yield call(authorityView,payload);
            if(result.code === 1){
                alert('添加成功');
            }else{
                alert('添加失败');
            }
        },
        *setIdentityApi({ payload }, { call, put }){
            let result = yield call(setIdentityApi,payload);
            if(result.code === 1){
                alert('设置成功');
            }else{
                alert('设置失败');
            }
        },
        *setIdentityView({ payload }, { call, put }){
            let result = yield call(setIdentityView,payload);
            if(result.code ===1){
                alert('设置成功');
            }else{
                alert('设置失败');
            }
        },

    },
    reducers: {
        save(state, action) {
            return {
              ...state,
              ...action.payload,
            };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
              
            });
        }
    }
}

export default UserModel;
